//Decoder.scala
package test

import chisel3._
import chisel3.util._

class Decoder(inst: UInt){

  val SLL  = (inst === BitPat("b0000000_?????_?????_001_?????_0110011"))
  val SLLI = (inst === BitPat("b0000000_?????_?????_001_?????_0010011"))
  val SRL  = (inst === BitPat("b0000000_?????_?????_101_?????_0110011"))
  val SRLI = (inst === BitPat("b0000000_?????_?????_101_?????_0010011"))
  val SRA  = (inst === BitPat("b0100000_?????_?????_101_?????_0110011"))
  val SRAI = (inst === BitPat("b0100000_?????_?????_101_?????_0010011"))

  val ADD   = (inst === BitPat("b0000000_?????_?????_000_?????_0110011"))
  val ADDI  = (inst === BitPat("b???????_?????_?????_000_?????_0010011"))
  val SUB   = (inst === BitPat("b0100000_?????_?????_000_?????_0110011"))
  val LUI   = (inst === BitPat("b???????_?????_?????_???_?????_0110111"))
  val AUIPC = (inst === BitPat("b???????_?????_?????_???_?????_0010111"))

  val XOR  = (inst === BitPat("b0000000_?????_?????_100_?????_0110011"))
  val XORI = (inst === BitPat("b???????_?????_?????_100_?????_0010011"))
  val OR   = (inst === BitPat("b0000000_?????_?????_110_?????_0110011"))
  val ORI  = (inst === BitPat("b???????_?????_?????_110_?????_0010011"))
  val AND  = (inst === BitPat("b0000000_?????_?????_111_?????_0110011"))
  val ANDI = (inst === BitPat("b???????_?????_?????_111_?????_0010011"))

  val SLT   = (inst === BitPat("b0000000_?????_?????_010_?????_0110011"))
  val SLTI  = (inst === BitPat("b???????_?????_?????_010_?????_0010011"))
  val SLTU  = (inst === BitPat("b0000000_?????_?????_011_?????_0110011"))
  val SLTIU = (inst === BitPat("b???????_?????_?????_011_?????_0010011"))

  val BEQ  = (inst === BitPat("b???????_?????_?????_000_?????_1100011"))
  val BNE  = (inst === BitPat("b???????_?????_?????_001_?????_1100011"))
  val BLT  = (inst === BitPat("b???????_?????_?????_100_?????_1100011"))
  val BGE  = (inst === BitPat("b???????_?????_?????_101_?????_1100011"))
  val BLTU = (inst === BitPat("b???????_?????_?????_110_?????_1100011"))
  val BGEU = (inst === BitPat("b???????_?????_?????_111_?????_1100011"))

  val JAL  = (inst === BitPat("b???????_?????_?????_???_?????_1101111"))
  val JALR = (inst === BitPat("b???????_?????_?????_000_?????_1100111"))

  val LW = (inst === BitPat("b???????_?????_?????_010_?????_0000011"))
  val SW = (inst === BitPat("b???????_?????_?????_010_?????_0100011"))

  val MUL = (inst === BitPat("b0000001_?????_?????_000_?????_0110011"))

  val Shifts = Cat(false.B, SRAI, SRA, SRLI, SRL, SLLI, SLL, false.B)
  val Arithmetic = Cat(false.B, false.B, false.B, AUIPC, LUI, SUB, ADDI, ADD)
  val Logical = Cat(false.B, false.B, ANDI, AND, ORI, OR, XORI, XOR)
  val CompareM = Cat(MUL, false.B, false.B, false.B, SLTIU, SLTU, SLTI, SLT)
  val aluop = Cat(CompareM, Logical, Arithmetic, Shifts).asUInt

  val wen = (LW | JAL | JALR |
    SLT | SLTI | SLTU | SLTIU |
    XOR | XORI | OR | ORI | AND | ANDI |
    ADD | ADDI | SUB | LUI | AUIPC |
    SLL | SLLI | SRL | SRLI | SRA | SRAI |  MUL)

  val branch = BEQ | BNE | BLT | BGE | BLTU | BGEU
  val jump = JAL | JALR

  val load = LW
  val store = SW

}

